<?php


namespace app\admin\controller;


use app\common\controller\AdminController;
use app\model\Banner as BannerModel;
use think\App;

/**
 * @ControllerAnnotation(title="Banner管理")
 */
class Banner extends AdminController
{
    public ?BannerModel $model = null;

    public function __construct(App $app)
    {
        $this->model = new BannerModel();
        parent::__construct($app);
    }

    /**
     * @NodeAnotation(title="Banner列表")
     */
    public function bannerList()
    {
        $title = input('title','', 'trim');
        $type = input('type',0, 'int');
        $page = input('page', 1, 'int');
        $pageSize = input('pageSize', 20, 'int');
        $ids = input('ids', '', 'trim');
        $export = input('export', 0, 'int');

        $where = [];
        $where[] = ['delete_time', '=', 0];
        if ($title) {
            $where[] = ['title', 'LIKE', "%{$title}%"];
        }
        if ($type) {
            $where[] = ['type', '=', $type];
        }
        if ($ids) {
            $where[] = ['id', 'IN', explode(',', $ids)];
        }
        $total = $this->model->where($where)->value('count(1)');
        $list = [];
        if ($total > 0) {
            $list = $this->model->where($where)->page($page, $pageSize)->order('sort desc,id asc')->select()->toArray();
            foreach ($list as $key => $val) {
                $list[$key]['status_name'] = $val['status'] ? '开启' : '关闭';
                $list[$key]['image'] = get_image_url($val['image']);
                $list[$key]['type_name'] = BannerModel::$type_name[$val['type']] ?? '';
            }
        }

        //导出
        if ($export) {
            $url = generateExcelFile($list, [
                'type_name'=> '类型',
                'title'=> '标题',
                'image'=> '图片',
                'url'=> '跳转路由',
                'status_name'=> '状态',
                'sort'=> '排序',
                'create_at'=> '添加时间',
            ]);
            $this->success(['url'=> $url]);
        }

        $typeList = [];
        foreach (BannerModel::$type_name as $key => $val) {
            $typeList[] = ['key'=> $key, 'name'=> $val, 'px'=> BannerModel::$px_text[$key]??''];
        }

        $res = [
            'total'=> $total,
            'list'=> $list,
            'typeList'=> $typeList,
        ];
        $this->success($res);
    }

    /**
     * @NodeAnotation(title="添加编辑Banner")
     */
    public function bannerEdit()
    {
        $id = input('id', 0, 'int');
        $title = input('title', '', 'trim');
        $type = input('type', 0, 'int');
        $image = input('image', '', 'trim');
        $url = input('url', '', 'trim');
        $sort = input('sort', 0, 'int');
        $data = [
            'title' => $title,
            'type' => $type,
            'image' => get_image_path($image),
            'url' => $url,
            'sort' => $sort,
        ];
        // 验证
        $this->validate($data,[
            'title|标题' => 'require',
            'type|类型' => 'require',
            'image|图片' => 'require',
        ]);

        if ($id) {
            $this->model->where(['id'=> $id])->update($data);
        } else {
            $data['create_time'] = time();
            $this->model->insert($data);
        }
        $this->success([],'保存成功');
    }

    /**
     * @NodeAnotation(title="删除Banner")
     */
    public function bannerDel()
    {
        $id = input('id', 0, 'int');
        if (empty($id)) {
            $this->error('参数缺失');
        }
        $this->model->where(['id'=> $id])->update(['delete_time'=>time()]);
        $this->success([], '删除成功');
    }

    /**
     * @NodeAnotation(title="设置Banner状态")
     */
    public function bannerEditStatus()
    {
        $id = input('id', '', 'trim');
        $status = input('status') ? 1 : 0;
        if (empty($id)) {
            $this->error('参数缺失');
        }
        $ids = explode(',', $id);
        $update = ['status'=> $status];
        foreach ($ids as $id) {
            $this->model->where(['id'=> (int)$id])->update($update);
        }
        $this->success([], '设置成功');
    }
}